home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectShow_WinXP / VMR / TxtPlayer / commands.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  9.3 KB  |  394 lines

  1. //------------------------------------------------------------------------------
  2. // File: commands.cpp
  3. //
  4. // Desc: DirectShow sample code
  5. //       - Processes commands from the user.
  6. //
  7. // Copyright (c) 1994 - 2001, Microsoft Corporation.  All rights reserved.
  8. //------------------------------------------------------------------------------
  9.  
  10. #include <streams.h>
  11. #include <mmreg.h>
  12. #include <commctrl.h>
  13.  
  14. #include "project.h"
  15. #include "mpgcodec.h"
  16. #include <stdio.h>
  17.  
  18.  
  19. extern void RepositionMovie(HWND hwnd);
  20. extern CMpegMovie *pMpegMovie;
  21.  
  22.  
  23. /******************************Public*Routine******************************\
  24. * VcdPlayerOpenCmd
  25. *
  26. \**************************************************************************/
  27. BOOL
  28. VcdPlayerOpenCmd(
  29.     void
  30.     )
  31. {
  32.     static BOOL fFirstTime = TRUE;
  33.     BOOL fRet;
  34.     TCHAR achFileName[MAX_PATH];
  35.     TCHAR achFilter[MAX_PATH];
  36.     LPTSTR lp;
  37.  
  38.     if(fFirstTime)
  39.     {
  40.         ofn.lStructSize = sizeof(ofn);
  41.         ofn.hwndOwner = hwndApp;
  42.         ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST |
  43.             OFN_SHAREAWARE | OFN_PATHMUSTEXIST;
  44.     }
  45.  
  46.     lstrcpy(achFilter, IdStr(STR_FILE_FILTER));
  47.     ofn.lpstrFilter = achFilter;
  48.  
  49.     /*
  50.     ** Convert the resource string into to something suitable for
  51.     ** GetOpenFileName ie.  replace '#' characters with '\0' characters.
  52.     */
  53.     for(lp = achFilter; *lp; lp++)
  54.     {
  55.         if(*lp == TEXT('#'))
  56.         {
  57.             *lp = TEXT('\0');
  58.         }
  59.     }
  60.  
  61.     ofn.lpstrFile = achFileName;
  62.     ofn.nMaxFile = sizeof(achFileName) / sizeof(TCHAR);
  63.     ZeroMemory(achFileName, sizeof(achFileName));
  64.  
  65.     fRet = GetOpenFileName(&ofn);
  66.     if(fRet)
  67.     {
  68.         fFirstTime = FALSE;
  69.         ProcessOpen(achFileName);
  70.     }
  71.  
  72.     return fRet;
  73. }
  74.  
  75.  
  76. /******************************Public*Routine******************************\
  77. * VcdPlayerCloseCmd
  78. *
  79. \**************************************************************************/
  80. BOOL
  81. VcdPlayerCloseCmd(
  82.     void
  83.     )
  84. {
  85.     if(pMpegMovie)
  86.     {
  87.         LONG cx, cy;
  88.  
  89.         g_State = VCD_NO_CD;
  90.         pMpegMovie->GetMoviePosition(&lMovieOrgX, &lMovieOrgY, &cx, &cy);
  91.         pMpegMovie->StopMovie();
  92.         pMpegMovie->CloseMovie();
  93.  
  94.         SetDurationLength((REFTIME)0);
  95.         SetCurrentPosition((REFTIME)0);
  96.  
  97.         delete pMpegMovie;
  98.         pMpegMovie = NULL;
  99.     }
  100.  
  101.     InvalidateRect(hwndApp, NULL, FALSE);
  102.     UpdateWindow(hwndApp);
  103.     return TRUE;
  104. }
  105.  
  106.  
  107. /******************************Public*Routine******************************\
  108. * VcdPlayerPlayCmd
  109. *
  110. \**************************************************************************/
  111. BOOL
  112. VcdPlayerPlayCmd(
  113.     void
  114.     )
  115. {
  116.     BOOL fStopped = (g_State & VCD_STOPPED);
  117.     BOOL fPaused  = (g_State & VCD_PAUSED);
  118.  
  119.     if((fStopped || fPaused))
  120.     {
  121.         if(pMpegMovie)
  122.         {
  123.             pMpegMovie->PlayMovie();
  124.         }
  125.  
  126.         g_State &= ~(fStopped ? VCD_STOPPED : VCD_PAUSED);
  127.         g_State |= VCD_PLAYING;
  128.     }
  129.  
  130.     return TRUE;
  131. }
  132.  
  133.  
  134. /******************************Public*Routine******************************\
  135. * VcdPlayerPlayCmd
  136. *
  137. \**************************************************************************/
  138. BOOL
  139. VcdPlayerStopCmd(
  140.     void
  141.     )
  142. {
  143.     BOOL fPlaying = (g_State & VCD_PLAYING);
  144.     BOOL fPaused  = (g_State & VCD_PAUSED);
  145.  
  146.     if((fPlaying || fPaused))
  147.     {
  148.         if(pMpegMovie)
  149.         {
  150.             pMpegMovie->StopMovie();
  151.             SetCurrentPosition(pMpegMovie->GetCurrentPosition());
  152.         }
  153.  
  154.         g_State &= ~(fPlaying ? VCD_PLAYING : VCD_PAUSED);
  155.         g_State |= VCD_STOPPED;
  156.     }
  157.     return TRUE;
  158. }
  159.  
  160.  
  161. /******************************Public*Routine******************************\
  162. * VcdPlayerStepCmd
  163. *
  164. \**************************************************************************/
  165. BOOL
  166. VcdPlayerStepCmd(
  167.     void
  168.     )
  169. {
  170.     if(pMpegMovie)
  171.     {
  172.         // Ensure that the video is paused to update toolbar buttons
  173.         if(g_State & VCD_PLAYING)
  174.             VcdPlayerPauseCmd();
  175.  
  176.         if(pMpegMovie->FrameStepMovie())
  177.         {
  178.             g_State |= VCD_STEPPING;
  179.             return TRUE;
  180.         }
  181.     }
  182.     return FALSE;
  183. }
  184.  
  185.  
  186. /******************************Public*Routine******************************\
  187. * VcdPlayerPauseCmd
  188. *
  189. \**************************************************************************/
  190. BOOL
  191. VcdPlayerPauseCmd(
  192.     void
  193.     )
  194. {
  195.     BOOL fPlaying = (g_State & VCD_PLAYING);
  196.     BOOL fPaused  = (g_State & VCD_PAUSED);
  197.  
  198.     if(fPlaying)
  199.     {
  200.         if(pMpegMovie)
  201.         {
  202.             pMpegMovie->PauseMovie();
  203.             SetCurrentPosition(pMpegMovie->GetCurrentPosition());
  204.         }
  205.  
  206.         g_State &= ~VCD_PLAYING;
  207.         g_State |= VCD_PAUSED;
  208.     }
  209.     else if(fPaused)
  210.     {
  211.         if(pMpegMovie)
  212.         {
  213.             pMpegMovie->PlayMovie();
  214.         }
  215.  
  216.         g_State &= ~VCD_PAUSED;
  217.         g_State |= VCD_PLAYING;
  218.     }
  219.  
  220.     return TRUE;
  221. }
  222.  
  223. /******************************Public*Routine******************************\
  224. * VcdPlayerSeekCmd
  225. *
  226. \**************************************************************************/
  227. void
  228. VcdPlayerSeekCmd(
  229.     REFTIME rtSeekBy
  230.     )
  231. {
  232.     REFTIME rt;
  233.     REFTIME rtDur;
  234.  
  235.     rtDur = pMpegMovie->GetDuration();
  236.     rt = pMpegMovie->GetCurrentPosition() + rtSeekBy;
  237.  
  238.     rt = max(0, min(rt, rtDur));
  239.  
  240.     pMpegMovie->SeekToPosition(rt,TRUE);
  241.     SetCurrentPosition(pMpegMovie->GetCurrentPosition());
  242. }
  243.  
  244.  
  245. /******************************Public*Routine******************************\
  246. * ProcessOpen
  247. *
  248. \**************************************************************************/
  249. void
  250. ProcessOpen(
  251.     TCHAR *achFileName,
  252.     BOOL bPlay
  253.     )
  254. {
  255.     /*
  256.     ** If we currently have a video loaded we need to discard it here.
  257.     */
  258.     if(g_State & VCD_LOADED)
  259.     {
  260.         VcdPlayerCloseCmd();
  261.     }
  262.  
  263.     lstrcpy(g_achFileName, achFileName);
  264.  
  265.     pMpegMovie = new CMpegMovie(hwndApp);
  266.  
  267.     if(pMpegMovie)
  268.     {
  269.         HRESULT hr = pMpegMovie->OpenMovie(g_achFileName);
  270.         if(SUCCEEDED(hr))
  271.         {
  272.             TCHAR achTmp[MAX_PATH];
  273.  
  274.             nRecentFiles = SetRecentFiles(achFileName, nRecentFiles);
  275.  
  276.             wsprintf(achTmp, IdStr(STR_APP_TITLE_LOADED),
  277.                 g_achFileName);
  278.             g_State = (VCD_LOADED | VCD_STOPPED);
  279.  
  280.             // SetDurationLength(pMpegMovie->GetDuration());
  281.             g_TimeFormat = VcdPlayerChangeTimeFormat(g_TimeFormat);
  282.  
  283.             RepositionMovie(hwndApp);
  284.             InvalidateRect(hwndApp, NULL, FALSE);
  285.  
  286.             //  If play option specified on the command line
  287.             if(bPlay)
  288.             {
  289.                 pMpegMovie->PlayMovie();
  290.             }
  291.         }
  292.         else
  293.         {
  294.             TCHAR Buffer[MAX_ERROR_TEXT_LEN];
  295.  
  296.             if(AMGetErrorText(hr, Buffer, MAX_ERROR_TEXT_LEN))
  297.             {
  298.                 MessageBox(hwndApp, Buffer,
  299.                     IdStr(STR_APP_TITLE), MB_OK);
  300.             }
  301.             else
  302.             {
  303.                 MessageBox(hwndApp,
  304.                     TEXT("Failed to open the movie! ")
  305.                     TEXT("Either the file was not found or the wave device is in use."),
  306.                     IdStr(STR_APP_TITLE), MB_OK);
  307.             }
  308.  
  309.             pMpegMovie->CloseMovie();
  310.             delete pMpegMovie;
  311.             pMpegMovie = NULL;
  312.         }
  313.     }
  314.  
  315.     InvalidateRect(hwndApp, NULL, FALSE);
  316.     UpdateWindow(hwndApp);
  317. }
  318.  
  319.  
  320. /******************************Public*Routine******************************\
  321. * VcdPlayerChangeTimeFormat
  322. *
  323. * Tries to change the time format to id.  Returns the time format that
  324. * actually got set.  This may differ from id if the graph does not support
  325. * the requested time format.
  326. *
  327. \**************************************************************************/
  328. int
  329. VcdPlayerChangeTimeFormat(
  330.     int id
  331.     )
  332. {
  333.     // Menu items are disabled while we are playing
  334.     BOOL    bRet = FALSE;
  335.     int     idActual = id;
  336.  
  337.     ASSERT(pMpegMovie);
  338.     ASSERT(pMpegMovie->StatusMovie() != MOVIE_NOTOPENED);
  339.  
  340.     // Change the time format with the filtergraph
  341.     switch(id)
  342.     {
  343.         case IDM_FRAME:
  344.             bRet = pMpegMovie->SetTimeFormat(TIME_FORMAT_FRAME);
  345.             break;
  346.  
  347.         case IDM_FIELD:
  348.             bRet = pMpegMovie->SetTimeFormat(TIME_FORMAT_FIELD);
  349.             break;
  350.  
  351.         case IDM_SAMPLE:
  352.             bRet = pMpegMovie->SetTimeFormat(TIME_FORMAT_SAMPLE);
  353.             break;
  354.  
  355.         case IDM_BYTES:
  356.             bRet = pMpegMovie->SetTimeFormat(TIME_FORMAT_BYTE);
  357.             break;
  358.     }
  359.  
  360.     if(!bRet)
  361.     {
  362.         // IDM_TIME and all other cases,  everyone should support IDM_TIME
  363.         bRet = pMpegMovie->SetTimeFormat(TIME_FORMAT_MEDIA_TIME);
  364.         ASSERT(bRet);
  365.         idActual = IDM_TIME;
  366.     }
  367.  
  368.     // Pause the movie to get a current position
  369.     SetDurationLength(pMpegMovie->GetDuration());
  370.     SetCurrentPosition(pMpegMovie->GetCurrentPosition());
  371.  
  372.     return idActual;
  373. }
  374.  
  375.  
  376.  
  377. /******************************Public*Routine******************************\
  378. * VcdPlayerRewindCmd
  379. *
  380. \**************************************************************************/
  381. BOOL
  382. VcdPlayerRewindCmd(
  383.     void
  384.     )
  385. {
  386.     if(pMpegMovie)
  387.     {
  388.         pMpegMovie->SeekToPosition((REFTIME)0,FALSE);
  389.     }
  390.  
  391.     return TRUE;
  392. }
  393.  
  394.